package com.ayoka.parenting.ui.grown.detail;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.BasicUserInfoBean;
import com.ayoka.parenting.bean.ClassroomDetailBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.interf.SimpleTICCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.ayoka.parenting.widget.ConfirmDialog;
import com.ayoka.parenting.widget.NotificationDialog;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.utils.UIUtils;
import com.tencent.ticsdk.core.TICManager;

import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GrownDetailPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GrownDetailPresenter extends BasePresenterImpl<GrownDetailContract.View> implements GrownDetailContract.Presenter{

    private GrownDetailModel model;

     public GrownDetailPresenter() {
        model = new GrownDetailModel();
     }

    @Override
    public void getClassroomDetailData(String classroomId) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.getClassroomDetailData(this, classroomId, new SimpleRequestCallback<ClassroomDetailBean>() {

            @Override
            public void onSuccess(ClassroomDetailBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configClassroomData(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public List<BasicUserInfoBean> getCoachListData() {
        return model.getCoachListData();
    }


    @Override
    public void showConfirmJoinDialog(final FragmentManager fragmentManager, final String classroomId, String coachName, int classroomType) {
        String message = "你确定要报名" + coachName + "开设的体验课吗？";
        if (classroomType == Constants.ClassroomType.GROW) {
            message = "每个哈巴会员限报名一个教练的线上成长营，确定要报名吗？";
        }
        final ConfirmDialog dialog = new ConfirmDialog.Builder()
                .setGravity(Gravity.CENTER)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setTitle("提示")
                .setMessage(message)
                .setButtonType(ConfirmDialog.DIALOG_BUTTON_CONFIRM_AND_CANCEL)
                .build();
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialogFragment, Object result) {
                dialogFragment.dismissAllowingStateLoss();
                if (TextUtils.equals("CONFIRM", (String)result)){
                    if (!isAttached()) return;
                    UIUtils.postTaskDelay(new Runnable() {
                        @Override
                        public void run() {
                            showNotificationDialog(fragmentManager, classroomId);
                        }
                    }, 200);
                }
            }
        });
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached()) return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.show(fragmentManager, "ConfirmDialog");
    }

    private void showNotificationDialog(FragmentManager fragmentManager, final String classroomId) {
        String message = "理念：\n" +
                "\t本人自愿报名参加21次线上成长营参加契约，并怀有将育儿中的遇到的问题和挑战，视为自我情绪成长机会的强烈愿望；让自己成长为内心更平和、耐心、智慧、情绪更成熟的父母；成为孩子的榜样；愿意与他人交流，并具有与他人交流的能力；表现真正的自我，探索发掘本身具备的智慧父母品质；陪孩子一起快乐成长。<br/><br/>\\\n" +
                "<h4>目标：\n" +
                "1.透过21次成长营支持父母们更多地觉察到自己哪些行为、态度与情绪反应导致了无效的养育方式。\n" +
                "2.协助父母认出并碰触到自己本来就拥有的天赋与品质，并以它们为指引，用智慧又有效的方式去对待孩子，爱孩子；\n" +
                "3.在放松中学习，获得体验式的成长；<br/><br/>\n" +
                "在参加由老师带领的第期的《21次线上成长营》团体课程,保证积极参与21次成长课，并遵守以下课程约定：\n" +
                "1.准时参加每次线上成长营，不迟到，不无故缺席。\n" +
                "2.如果遇到个人无法控制的意外情况不能到课，做到提前请假。\n" +
                "3.上课期间确保注意力集中在课程中。\n" +
                "4.全身心投入，真诚、坦率，直言不讳，真实开放自己，愿意不断成长。\n" +
                "5.坦诚对待团体中的每一位成员，互相尊重，学会倾听、经验分享，相互信赖。\n" +
                "6.课后承诺严格遵守保密原则，尊重每一位成员个人的经历和隐私权。";
        final NotificationDialog dialog = new NotificationDialog.Builder()
                .setGravity(Gravity.CENTER)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setTitle("21次线上成长营告知书")
                .setMessage(message)
                .setNegativeText("放弃")
                .setPositiveText("遵守")
                .build();
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialogFragment, Object result) {
                dialogFragment.dismissAllowingStateLoss();
                if (!isAttached()) return;
                joinClassroom(classroomId);
            }
        });
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached()) return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.show(fragmentManager, "NotificationDialog");
    }

    private void joinClassroom(String classroomId) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.joinClassroom(this, classroomId, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onJoinClassroomCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onJoinClassroomCallback(false);
            }
        });
    }

    @Override
    public void createLiveRoom(final int roomId, final String scheduleId) {
        mView.get().showWaitDialog("正在进入房间！");
        TICManager.getInstance().createClassroom(roomId, new SimpleTICCallback() {
            @Override
            public void onSuccess(Object data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
//                    mViewRef.get().showToast("创建课堂 成功, 房间号："  + mRoomId);
                mView.get().onCreateLiveRoomCallback(true, roomId, scheduleId);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                if (errorCode == 6014) {
                    createLiveRoom(roomId, scheduleId);
                } else if (errorCode == 10021) {
//                        mViewRef.get().showToast("该课堂已被他人创建，请\"加入课堂\"");
                    mView.get().onCreateLiveRoomCallback(true, roomId, scheduleId);
                } else if (errorCode == 10025) {
//                        mViewRef.get().showToast("该课堂已创建，请\"加入课堂\"");
                    mView.get().onCreateLiveRoomCallback(true, roomId, scheduleId);
                } else {
                    mView.get().showToast("创建课堂失败, 房间号：" + roomId + " err:" + errorCode + " msg:" + errorMessage);
                    mView.get().onCreateLiveRoomCallback(false, roomId, scheduleId);
                }
            }
        });
    }
}
